package com.deductive.web.disposition.wq;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.utils.DateUtils;
import com.deductive.domain.special.DispositionWqCompilation;
import com.deductive.domain.special.SpecialDataTree;
import com.deductive.service.wq.DispositionWqCompilationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/dispositionWqCompilation")
@Api(tags = "WQ-编制力量管理(需要对接医院和远离体系表中的数据)")
public class DispositionWqCompilationController extends BaseController {

    @Resource
    private DispositionWqCompilationService dispositionWqCompilationService;

    @GetMapping("/list")
    @ApiOperation("编制力量目录查询")
    public AjaxResult list(DispositionWqCompilation dispositionWqCompilation) {
        LambdaQueryWrapper<DispositionWqCompilation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dispositionWqCompilation.getName()!=null,DispositionWqCompilation::getName,dispositionWqCompilation.getName())
                .orderByAsc(DispositionWqCompilation::getOrderNum);
        List<DispositionWqCompilation> list = dispositionWqCompilationService.list(queryWrapper);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setWeightKey("orderNum");

        List<Tree<Object>> build = TreeUtil.build(list, 0, treeNodeConfig, (map, tree) -> {
            tree.setId(map.getId());
            tree.setName(map.getName());
            tree.setParentId(map.getParentId());
            tree.putExtra("relevanceTable", map.getRelevanceTable());
            tree.putExtra("relevanceColumn", map.getRelevanceColumn());
            tree.putExtra("orderNum", map.getOrderNum());
        });
        if (build == null || build.isEmpty()) {
            // 当没有可用的父子关系时，按平铺返回节点，避免返回空
            List<Tree<Object>> flat = new ArrayList<>();
            for (DispositionWqCompilation map : list) {
                Tree<Object> tree = new Tree<>();
                tree.setId(map.getId());
                tree.setName(map.getName());
                tree.setParentId(map.getParentId());
                tree.putExtra("relevanceTable", map.getRelevanceTable());
                tree.putExtra("relevanceColumn", map.getRelevanceColumn());
                tree.putExtra("orderNum", map.getOrderNum());
                flat.add(tree);
            }
            return AjaxResult.success(flat);
        }
        return AjaxResult.success(build);
    }

    @PutMapping("/update")
    @ApiOperation("编制力量修改")
    public AjaxResult update(@RequestBody DispositionWqCompilation dispositionWqCompilation) {
        try
        {
            dispositionWqCompilation.setUpdateTime(DateUtils.getNowDate());
            return AjaxResult.success(dispositionWqCompilationService.updateById(dispositionWqCompilation));
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage(),false);
        }
    }

    @PostMapping("/insert")
    @ApiOperation("编制力量新增")
    public AjaxResult insert(@RequestBody DispositionWqCompilation dispositionWqCompilation) {
        try
        {
            dispositionWqCompilation.setUpdateTime(DateUtils.getNowDate());
            return AjaxResult.success(dispositionWqCompilationService.save(dispositionWqCompilation));
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage(),false);
        }
    }

    @DeleteMapping
    @ApiOperation("编制力量删除")
    public AjaxResult delete(@RequestParam("idList") List<Integer> idList) {
        return AjaxResult.success(dispositionWqCompilationService.removeByIds(idList));
    }

}
